<template>
  <div>
    <!-- 投保信息 -->
    <el-dialog
      title="投保信息"
      :visible.sync="visible"
      width="60%"
      append-to-body
      @closed="reset"
    >
      <div class="title">
        <span class="title-icon" />
        <span class="title-text">投保地址</span>
      </div>
      <lb-table
        ref="lbTable"
        border
        highlight-current-row
        element-loading-text="加载中"
        :column="tableData.column"
        :data="tableData.data"
        @current-change="handleCurrentChange"
      />
      <el-form ref="dataForm" :rules="rules" style="margin-top: 10px;" class="form" :model="temp" label-position="right" label-width="90px">

        <!-- <el-form-item label-width="0" prop="qtype">
          <el-input v-model="param" placeholder="请输入" clearable>
            <el-select slot="prepend" v-model="qtype" style="width: 100px;" placeholder="请选择">
              <el-option label="电话" value="csmRgPhone" />
              <el-option label="客户编号" value="csmCode" />
              <el-option label="身份证号" value="sfzh" />
            </el-select>
            <el-button slot="append" class="append-search" @click="searchMessage">查询</el-button>
          </el-input>
        </el-form-item> -->

        <!-- <el-form-item label="客户名称" prop="csmName">
          <el-input v-model="temp.csmName" />
        </el-form-item> -->
        <el-form-item label="保单号" prop="code">
          <el-input v-model="temp.code" placeholder="请输入" clearable>
            <el-button slot="append" class="append-search" @click="getCode">获取</el-button>
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="投保地址" prop="address">
          <el-select v-model="temp.address" placeholder="请选择">
            <el-option
              v-for="item in address"
              :key="item.csmAddressId"
              :label="item.csmAddress"
              :value="item.csmAddressId"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="投保金额" prop="money">
          <!-- <el-input v-model="temp.money" placeholder="请输入" clearable /> -->
          <el-input v-model.trim="temp.money" :maxlength="16" placeholder="请输入" clearable @input="onInput($event)" @blur="formatInput($event)" />
        </el-form-item>
        <el-form-item label="售保员" prop="saleperson">
          <el-input v-model="temp.saleperson" placeholder="请输入" clearable />
        </el-form-item>
        <!-- <el-form-item label="生效日期" prop="date" :table-form-merge="2" style="width: 100%;">
          <el-date-picker
            v-model="temp.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item> -->
        <el-form-item label="生效日期:" prop="insureStartDt">
          <el-date-picker
            v-model="temp.insureStartDt"
            type="date"
            placeholder="选择日期"
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="截止日期:" prop="insureEndDt">
          <el-date-picker
            v-model="temp.insureEndDt"
            type="date"
            placeholder="选择日期"
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item prop="remark" class="block">
          <label slot="label">备&emsp;&emsp;注</label>
          <el-input v-model="remark" type="textarea" :rows="3" maxlength="255" show-word-limit />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { queryCodeGeneration, insureSave, customerAddress } from '@/api'
  import { parseTime } from '@/utils'

  export default {
    name: 'insure',
    data() {
      return {
        visible: false,
        remark: '',
        qtype: 'csmRgPhone',
        rules: {
          money: [{ required: true, message: '请输入投保金额', trigger: 'change' }],
          insureStartDt: [{ required: true, message: '请选择生效时间', trigger: 'change' }],
          insureEndDt: [{ required: true, message: '请选择截至时间', trigger: 'change' }]
          // orderCode: [{ required: true, message: '请输入押金单号', trigger: 'change' }],
          // address: [{ required: true, message: '请选择投保地址', trigger: 'change' }],
          // price: [{ required: true, message: '请输入投保金额', trigger: 'change' }]
        },
        // date: [],
        addressData: null,
        tableData: {
          total: 0,
          column: [
            { prop: 'csmAddress', label: '地址', width: '', align: 'left' },
            { prop: 'insureCode', label: '保单号', width: 140, align: 'center' },
            { prop: 'insureStartDt', label: '生效日期', width: '', align: 'center',
              render: (h, { row }) => <span>{ row.insureStartDt && parseTime(row.insureStartDt, '{y}-{m}-{d}') }</span>
            },
            { prop: 'insureEndDt', label: '截止日期', width: '', align: 'center',
              render: (h, { row }) => <span>{ row.insureEndDt && parseTime(row.insureEndDt, '{y}-{m}-{d}') }</span>
            }
          ],
          data: []
        },
        temp: {
          code: '',
          csmId: '',
          saleperson: '',
          money: '',
          csmPhone: '',
          select: 1,
          csmStatus: 1,
          csmName: '',
          csmCode: '',
          csmType: 1,
          gasNature: '',
          csmRgPhone: '',
          contact: '',
          orgName: '',
          csmFloor: 1,
          unCheckQuality: '',
          sfzh: '',
          sfzName: '',
          sfzAddress: '',
          cardType: '',
          cardNo: '',
          newType: 2,
          defaultPaymentMethod: 1,
          newFrom: 8,
          salesmanName: '',
          isPickUp: 0,
          settlementType: 1,
          createTime: '',
          contractNumber: '',
          decimalSpare1: '',
          insureStartDt: '',
          insureEndDt: ''
        }
      }
    },
    created() {
      // this.getData()
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          console.log({ ...temp }, 'temp')
          this.temp = Object.assign(this.temp, temp)
          this.getData()
          // this.address = Object.assign(this.address, res.data)
        })
      },
      // 获取保单号
      async getCode() {
        const res = await queryCodeGeneration({ codeType: 10 })
        this.temp.code = res.data
      },
      // 获取地址信息
      async  getData() {
        const params = {
          csmId: this.temp.csmId
        }
        const res = await customerAddress(params)
        this.tableData.data = res.data
      },
      handleCurrentChange(val) {
        this.addressData = val
        console.log('投保地址', val)
      },
      // 保存
      async save() {
        if (!this.addressData) {
          this.$message.warning('请先选择要投保的地址!')
          return
        }
        this.$refs['dataForm'].validate(async(valid) => {
          if (valid) {
            const csmInfo = {
              csmId: this.temp.csmId,
              csmAddressId: this.addressData.csmAddressId,
              csmAddress: this.addressData.csmAddress,
              ckOrgId: this.addressData.orgId,
              ckOrgName: this.addressData.orgName,
              ckOrgSNNo: this.addressData.orgSNNo,
              insureCode: this.temp.code,
              insureStartDt: this.temp.insureStartDt,
              insureEndDt: this.temp.insureEndDt,
              insureAcc: this.temp.money,
              salePersonName: this.temp.saleperson,
              remark: this.remark,
              csmName: this.temp.csmName
            }
            await insureSave(csmInfo)
            this.$message.success('保存成功')
            this.visible = false
            this.addressData = null
            this.$refs['dataForm'].resetFields()
          } else {
            // console.log('error submit!!')
            return false
          }
        })

        // console.log({ ...csmInfo }, 'params')

        // this.getData()
      },
      reset() {
        console.log('顶顶顶顶顶')
        this.visible = false
        this.qtype = 'csmRgPhone'
        this.remark = ''
        this.temp = {
          code: '',
          csmId: '',
          saleperson: '',
          money: '',
          csmPhone: '',
          select: 1,
          csmStatus: 1,
          csmName: '',
          csmCode: '',
          csmType: 1,
          gasNature: '',
          csmRgPhone: '',
          contact: '',
          orgName: '',
          csmFloor: 1,
          unCheckQuality: '',
          sfzh: '',
          sfzName: '',
          sfzAddress: '',
          cardType: '',
          cardNo: '',
          newType: 2,
          defaultPaymentMethod: 1,
          newFrom: 8,
          salesmanName: '',
          isPickUp: 0,
          settlementType: 1,
          createTime: '',
          contractNumber: '',
          decimalSpare1: ''
        }
        // this.$nextTick(() => {
        //   this.$refs.dataForm.clearValidate()
        // })
      },
      // 格式化金额
      formatInput(e) {
        if (e.target.value) {
          this.temp.money = (e.target.value * 1).toFixed(2)
        }
      },
      // 格式化金额
      onInput(value) {
        const res = value
          .replace(/[^\d.]/g, '')
          .replace(/\.{2,}/g, '.')
          .replace(/^\./g, '')
          .replace('.', '$#$')
          .replace(/\./g, '')
          .replace('$#$', '.')
          .replace(/^0\d+|^\./g, '')
          .replace(/^(.*\..{2}).*$/, '$1')
        this.temp.money = res
      }
    }
  }
</script>

<style lang='scss' scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  & /deep/ .el-form-item {
    width: 33%;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
.title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  .title-icon {
    display: inline-block;
    width: 4px;
    height: 18px;
    margin-right: 6px;
    background: #01999f;
  }
  .title-text {
    font-family: Microsoft YaHei, Microsoft YaHei-Bold;
    font-size: 14px;
    font-weight: 700;
    color: #01999f;
  }
  .content {
    margin-bottom: 0;
  }
}
</style>
